package cn.teacher.controller;

import cn.teacher.config.web.DataGridResultInfo;
import cn.teacher.config.web.ResponseMessage;
import cn.teacher.config.web.ResponseStatusCode;
import cn.teacher.pojo.dto.CompetitionPlanDTO;
import cn.teacher.pojo.dto.RegisterCompetitionDTO;
import cn.teacher.pojo.vo.JwtUser;
import cn.teacher.pojo.CompetitionPlan;
import cn.teacher.service.CompetitionPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/25
 * Time: 19:26
 * Description:
 */
@RestController
@RequestMapping("/competition")
public class CompetitionPlanController {

    @Autowired
    private CompetitionPlanService competitionPlanService;

    @PostMapping("/save")
    public ResponseMessage save(JwtUser user, CompetitionPlan competitionPlan) {
        System.out.println(user.toString());
        System.out.println(competitionPlan.toString());
        if (1 == user.getJwtIdentity()) {
            competitionPlan.setUserId(user.getJwtId());
            final boolean add = competitionPlanService.add(competitionPlan);
            if (add) {
                return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "添加成功", null);
            } else {
                return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "添加失败", null);
            }
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "非教师不可添加", null);
    }

    @PostMapping("/delete")
    public ResponseMessage delete(@RequestParam("cpId") long cpId) {
        final boolean remove = competitionPlanService.remove(cpId);
        if (remove) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "删除成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_NOT_EXISTS, "无该竞赛", null);
    }

    @PostMapping("/update")
    public ResponseMessage update(CompetitionPlan competitionPlan) {
        final boolean modify = competitionPlanService.modify(competitionPlan);
        if (modify) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "更新成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_NOT_EXISTS, "无该数据", null);
    }

    @PostMapping("/list")
    public ResponseMessage list(@RequestParam(value = "userId", defaultValue = "-1", required = false) int userId,
                                @RequestParam(value = "page", defaultValue = "1") int page,
                                @RequestParam(value = "rows", defaultValue = "5") int rows) {
        final DataGridResultInfo<CompetitionPlanDTO> dataGridResultInfo = competitionPlanService.find(userId, page, rows);
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "请求成功", dataGridResultInfo);
    }

    @PostMapping("/getRegisterCompetition")
    public ResponseMessage getRegisterCompetition(JwtUser user,
                                                  @RequestParam(value = "page", defaultValue = "1") int page,
                                                  @RequestParam(value = "rows", defaultValue = "5") int rows) {
        final long jwtId = user.getJwtId();
        final DataGridResultInfo<RegisterCompetitionDTO> dataGridResultInfo = competitionPlanService.findRegisterCompetitionDTO(jwtId, page, rows);
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "请求成功", dataGridResultInfo);
    }

    @PostMapping("/getCompetitionPlans")
    public ResponseMessage getCompetitionPlans(@RequestParam(value = "page", defaultValue = "1") int page,
                                               @RequestParam(value = "rows", defaultValue = "5") int rows) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "请求成功", competitionPlanService.findCompetitionPlans(page, rows));
    }

    @PostMapping("/getCompetitionPlan")
    public ResponseMessage getCompetitionPlan(@RequestParam("cpId") long cpId) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "请求成功", competitionPlanService.findCompetitionPlan(cpId));
    }
}
